package com.dom.invoapp.modular.provider;

import com.baomidou.mybatisplus.plugins.Page;
import com.dom.api.sale.SaleOrderApi;
import com.dom.common.enums.CancelStatEnum;
import com.dom.common.enums.SaleOrderStatEnum;
import com.dom.common.model.standard.ResponseModel;
import com.dom.common.model.vo.sale.*;
import com.dom.invoapp.dao.entity.TmMarketOrderFlow;
import com.dom.invoapp.modular.service.SaleOrderService;
import com.xiaoleilu.hutool.date.DateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 销售订单provider
 * @author zhuwei
 * @Date 2018-11-18
 */
@Slf4j
@RestController
public class SaleOrderProvider extends AbstractProvider implements SaleOrderApi {

    @Autowired
    private SaleOrderService saleOrderService;

    @Override
    public M019A getSaleOrderList(@RequestBody M019R request) {
        Page<TmMarketOrderFlow> page = saleOrderService.findByPage(request);
        if (page == null){
            return null;
        }
        M019A m019A = new M019A();
        m019A.setPn(page.getCurrent());
        m019A.setPs(page.getSize());
        m019A.setTotalNum((int)page.getTotal());
        m019A.setPages((int)page.getPages());
        List<SaleOrderInfo> list = new ArrayList<SaleOrderInfo>();
        for (TmMarketOrderFlow flow : page.getRecords()
             ) {
            SaleOrderInfo info = buildSale(flow);
            list.add(info);
        }
        m019A.setSaleOrderData(list);
        return m019A;
    }

    private SaleOrderInfo buildSale(TmMarketOrderFlow flow){
        SaleOrderInfo info = new SaleOrderInfo();
        info.setKeyId(flow.getId().toString());
        info.setAddress(flow.getAddress());
        info.setBillAbstract(flow.getDigest());
        info.setCancelStat(Integer.toString(flow.getCancelStat()));
        info.setCancelTime(flow.getCancelTime() == null ? "" : DateUtil.formatDate(flow.getCancelTime()));
        info.setCompCode(flow.getOrganCode() == null ? "" :flow.getOrganCode().toString());
        info.setCustCode(flow.getCustCode() == null ? "" : flow.getCustCode().toString());
        info.setCustName(flow.getAbbrName());
        info.setExchDate(flow.getExchDate() == null ? "" : DateUtil.formatDate(flow.getExchDate()));
        info.setLinkMan(flow.getContName());
        info.setLinkManTel(flow.getContPhone());
        info.setMakerId(flow.getDocuMakerId() == null ? "" : flow.getDocuMakerId().toString());
//        info.setMakerName();
//        info.setOrderDate(flow.get);
        info.setOrderId(flow.getOrderId());
//        info.setOrderTime(flow.);
        info.setOrderType(flow.getOrderType() == null ? null : flow.getOrderType().toString());
        info.setSaleMode(flow.getSaleType() == null ? null : flow.getSaleType().toString());
        info.setSendDate(flow.getSendDate() == null ? "" : DateUtil.formatDate(flow.getSendDate()));
        info.setVerfyDate(flow.getVerfyDate() == null ? "" : DateUtil.formatDate(flow.getVerfyDate()));
        info.setVerfyErrMsg(flow.getVerfyErrMsg());
        info.setVerfyId(flow.getVerfierId());
//        info.setVerfyName(flow.get);
        info.setVerfyStat(Integer.toString(flow.getVerfyStat()));
        return info;
    }

    @Override
    public M013A getMarketOrderDetailList(@RequestBody M013R request){
        return saleOrderService.findSaleDetail(request);
    }

    @Override
    public boolean saleOrderDel(@RequestParam("keyId") String keyId) {
        return saleOrderService.saleOrderDel(keyId);
    }

    @Override
    public ResponseModel saleOrderModify(@RequestBody M012R request) {
        return saleOrderService.saleOrderModify(request);
    }

    @Override
    public String insertSaleOrder(@RequestBody M010R request) {
        super.isValid(request);
        return saleOrderService.insertSaleOrder(request);
    }
}
